<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="学年名称" prop="schoolYearId">
        <el-select
          v-model="queryParams.schoolYearId"
          placeholder="请选择学年"
          clearable
          @change="searchTermByYear()"
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in yearOptions"
            :key="item.index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学期名称" prop="termId">
        <el-select
          v-model="queryParams.termId"
          placeholder="请选择学期"
          clearable
          size="small"
          style="width: 240px"
          @change="searchCollegeByTerm()"
        >
          <el-option
            v-for="item in termNameOptions"
            :key="item.index"
            :label="item.termName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>-->

      <el-form-item label="学院名称" prop="collegeId">
        <el-select
          v-model="queryParams.collegeId"
          placeholder="请选择学院"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in collegeOptions"
            :key="item.index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="职工号" prop="teacherNo">
        <el-input
          v-model="queryParams.teacherNo"
          placeholder="请输入职工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="性别码" prop="sex">
        <el-select
          v-model="queryParams.sex"
          placeholder="请选择性别码"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="联系电话" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="邮政编码" prop="postalCode">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.postalCode"-->
      <!--          placeholder="请输入邮政编码"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="电子信箱" prop="email">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.email"-->
      <!--          placeholder="请输入电子信箱"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="主页地址" prop="homepage">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.homepage"-->
      <!--          placeholder="请输入主页地址"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="showAddBtn">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:teacher:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:teacher:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:teacher:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:teacher:export']"
          >导出</el-button
        >
      </el-col>
       <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:teacher:export']"
          >批量导入</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="teacherList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="主键id" align="center" prop="teacherId" />-->
      <el-table-column label="职工号" align="center" prop="teacherNo" />
      <el-table-column label="姓名" align="center" prop="teacherName" />
      <el-table-column
        label="性别"
        align="center"
        prop="sex"
        :formatter="sexFormat"
      />
      <el-table-column label="所属学院" align="center" prop="dwmc" />
      <el-table-column label="状态" align="center" prop="dqzt" />
      <el-table-column label="职称" align="center" prop="jszc" />
      <el-table-column label="职位" align="center" prop="jzglbmc" />
      <!-- <el-table-column label="身份证号" align="center" prop="idCard" /> -->
      <el-table-column
        label="出生日期"
        align="center"
        prop="birthday"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="来校年月" align="center" prop="schoolYear" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.schoolYear, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="从教年月" align="center" prop="teachingYear" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.teachingYear, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="tel" />
      <el-table-column label="电子信箱" align="center" prop="email" /> -->
      <el-table-column
        label="操作"
        width="150px"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="showAddBtn"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:teacher:edit']"
            >修改</el-button
          >
          <el-button
            v-if="showAddBtn"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:teacher:remove']"
            >删除</el-button
          >
          <el-button
            v-if="showAddBtn"
            type="text"
            icon="el-icon-share"
            size="mini"
            @click="showInfo(scope.row)"
            >详情</el-button
          >
          <el-button
            v-if="!showAddBtn"
            size="mini"
            type="text"
            @click="selectTeacher(scope.row)"
            >选择</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教职工信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <!-- 添加教职工信息的步骤条 -->
      <!-- <div>
        <el-steps :active="addActive" align-center finish-status="success">
          <el-step title="教职工所在组织" description></el-step>
          <el-step title="教职工基础信息" description></el-step>
          <el-step title="提交" description></el-step>
        </el-steps>
        <div>
          <div style="width: 50%; margin: 20px auto">{{ addText }}</div> -->
      <!-- <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <div v-show="addActive===0">
              <el-form-item label="学年名称" prop="schoolYear">
                <el-select
                  v-model="form.schoolYearId"
                  placeholder="请选择学年"
                  clearable
                  size="small"
                  style=" width: 100%;"
                  @change="addTermByYear()"
                >
                  <el-option
                    v-for="item in yearOptions"
                    :key="item.index"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="学期名称" prop="termId">
                <el-select
                  v-model="form.termId"
                  placeholder="请选择学期"
                  clearable
                  size="small"
                  style="width: 100%;"
                  @change="addCollegeByTerm()"
                >
                  <el-option
                    v-for="item in termNameOptions"
                    :key="item.index"
                    :label="item.termName"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="学院名称" prop="collegeId">
                <el-select
                  v-model="form.collegeId"
                  placeholder="请选择学院"
                  clearable
                  size="small"
                  style="width: 100%;"
                >
                  <el-option
                    v-for="item in collegeOptions"
                    :key="item.index"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </div>
            <div v-show="addActive === 1"> -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div>
          <el-form-item label="职工号" prop="teacherNo">
            <el-input v-model="form.teacherNo" placeholder="请输入职工号" />
          </el-form-item>
          <el-form-item label="姓名" prop="teacherName">
            <el-input v-model="form.teacherName" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="状态" prop="dqzt">
            <el-select v-model="form.dqzt" placeholder="请选择状态">
              <el-option
                v-for="dict in teacherStatus"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="身份证号" prop="idCard">
            <el-input v-model="form.idCard" placeholder="请输入身份证号" />
          </el-form-item>
          <el-form-item label="出生日期" prop="birthday">
            <el-date-picker
              clearable
              size="small"
              v-model="form.birthday"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择出生日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="性别码" prop="sex">
            <el-select v-model="form.sex" placeholder="请选择性别码">
              <el-option
                v-for="dict in sexOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="参加工作年月" prop="workDate">
            <el-input
              v-model="form.workDate"
              placeholder="请输入参加工作年月"
            />
          </el-form-item>
          <el-form-item label="来校年月" prop="schoolYear">
            <el-date-picker
              clearable
              size="small"
              v-model="form.schoolYear"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择来校年月"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="从教年月" prop="teachingYear">
            <el-date-picker
              clearable
              size="small"
              v-model="form.teachingYear"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择从教年月"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="联系电话" prop="tel">
            <el-input v-model="form.tel" placeholder="请输入联系电话" />
          </el-form-item>
        </div>
      </el-form>
      <!-- </div>
      </div> -->

      <!-- <div class="neper">
        <el-button
          style="margin-top: 12px; margin-right: 10px"
          v-show="addActive > 0"
          @click="addper"
          >上一步</el-button
        >
        <el-button
          style="margin-top: 12px; margin-left: 10px"
          v-show="addActive < 2"
          @click="addnext"
          >下一步</el-button
        >
      </div> -->

      <!-- <div slot="footer" class="dialog-footer" v-show="addActive == 2">
        <el-button type="primary" @click="submitForm(), (addActive = 0)">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div> -->
      <div slot="footer" class="">
        <el-button type="primary" @click="submitForm()">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 预览教职工详情 -->
    <el-dialog :visible.sync="showDialogVisible" :loading="loading" width="80%">
      <show :row="row" v-if="showDialogVisible"></show>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTeacher,
  getTeacher,
  delTeacher,
  addTeacher,
  updateTeacher,
  exportTeacher,
} from "@/api/system/teacher";

// import { listTerm } from "@/api/org/term";
import { listCollege } from "@/api/org/college";
// import { listSchoolyear } from "@/api/org/schoolyear";
import show from "./show.vue";
import { org } from "@/views/mixins/org/index";

export default {
  name: "Teacher",
  components: { show },
  mixins: [org],
  props: {
    showAddBtn: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      //预览
      showDialogVisible: false,
      // yearOptions: [{}],
      addText: "",
      // 步骤条参数
      addActive: 0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教职工信息表格数据
      teacherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 性别码字典
      sexOptions: [],
      examTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        collegeId: null,
        teacherNo: null,
        teacherName: null,
        idCard: null,
        birthday: null,
        sex: null,
        departmentNumber: null,
        namePinyin: null,
        nameUsedBefore: null,
        nation: null,
        health: null,
        bloodType: null,
        faiths: null,
        hkNo: null,
        maritalStatus: null,
        nativePlace: null,
        birthplace: null,
        familyOrigin: null,
        brcf: null,
        homeAddress: null,
        currentAddress: null,
        registeredResidence: null,
        hkxz: null,
        culturalLevel: null,
        workDate: null,
        schoolYear: null,
        teachingYear: null,
        bzlbm: null,
        facultyCategory: null,
        zyh: null,
        teachingStatus: null,
        fileNumber: null,
        fileText: null,
        mailingAddress: null,
        tel: null,
        postalCode: null,
        email: null,
        homepage: null,
      },
      // 表单参数
      form: {
        collegeId: null,
      },
      // 表单校验
      rules: {
        teacherNo: [
          { required: true, message: "职工号不能为空", trigger: "blur" },
        ],
        teacherName: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
        ],
        sex: [{ required: true, message: "性别码不能为空", trigger: "change" }],
      },
      //学年信息
      // yearOptions: [],
      //  // 学期
      //  termNameOptions: [
      //   {
      //     termId: "",
      //     termName: "",
      //   },
      // ],
      //学院信息
      collegeOptions: [
        {
          id: "",
          name: "",
        },
      ],
      teacherStatus: [],
    };
  },
  created() {
    this.getList();
    // this.getYearList();
    this.getCollegeList();
    this.getDicts("sys_sex").then((response) => {
      this.sexOptions = response.data;
      console.log("sexOptions:", this.sexOptions);
    });

    this.getDicts("teacher_status").then((response) => {
      this.teacherStatus = response.data;
    });
  },
  methods: {
    //  resetTermOptions() {
    //   this.termNameOptions = [
    //     {
    //       termId: "",
    //       termName: "",
    //     },
    //   ];

    // },
    selectTeacher(row) {
      this.$emit("selectTeacher", row);
    },
    resetCollegeOptions() {
      this.collegeOptions = [
        {
          id: "",
          name: "",
        },
      ];
    },
    // // 获取学年列表
    // getYearList() {
    //   listSchoolyear().then(response => {
    //     this.yearOptions = response.rows
    //     console.log("yearOptions: ", this.yearOptions);
    //   });
    // },
    //  // 搜索栏 通过学期搜索学院
    //  searchCollegeByTerm() {
    //   this.queryParams.collegeId = "";
    //    this.resetCollegeOptions();
    //   if (this.queryParams.termId !== '') {
    //      listCollege({
    //        termId: this.queryParams.termId,
    //      }).then((res) => {
    //        this.collegeOptions = res.rows;
    //      });
    //    }
    // },
    //  // 添加 通过学期搜索学院
    // addCollegeByTerm() {
    //   this.form.collegeId = "";
    //   this.resetCollegeOptions();
    //   if (this.form.termId !== '') {
    //     listCollege({
    //       termId: this.form.termId,
    //     }).then((res) => {
    //       this.collegeOptions = res.rows;
    //     });
    //   }
    // },
    //  // 搜索栏通过学年获取学期
    //  searchTermByYear() {
    //   this.queryParams.termId = "";
    //   this.queryParams.collegeId = "";
    //   this.resetTermOptions();
    //   this.resetCollegeOptions();
    //   console.log(this.queryParams.schoolYearId);
    //   if(this.queryParams.schoolYearId!==''){
    //     listTerm({ schoolYearId: this.queryParams.schoolYearId }).then((res) => {
    //       this.termNameOptions = res.rows;
    //       console.log('termOption: ', res.rows);
    //     });
    //   }
    // },
    //  // 添加或者修改框
    //  addTermByYear() {
    //   this.form.termId = "";
    //   this.form.collegeId = "";
    //   this.resetTermOptions();
    //   this.resetCollegeOptions();
    //   console.log(this.form.schoolYearId);
    //   if(this.form.schoolYearId!==''){
    //     listTerm({ schoolYearId: this.form.schoolYearId }).then((res) => {
    //       this.termNameOptions = res.rows;
    //     });
    //   }
    // },
    // 用户打开弹出框的，初始化步骤条的信息
    closedDialog() {
      this.addActive = 0;
      this.addText = "以下为教职工所在组织的基本信息:";
    },
    // 用户点击添加考试中的下一步按钮
    addnext() {
      this.addActive++;
      if (this.addActive === 0) {
        this.addText = "以下为教职工所在组织的基本信息:";
      } else if (this.addActive === 1) {
        this.addText = "以下为教职工个人相关信息:";
      } else if (this.addActive === 2) {
        this.addText = "请提点击确定提交教职工信息";
      }
    },
    addper() {
      this.addActive--;
      if (this.addActive === 0) {
        this.addText = "以下为教职工所在组织的基本信息:";
      } else if (this.addActive === 1) {
        this.addText = "以下为教职工个人相关信息:";
      } else if (this.addActive === 2) {
        this.addText = "请提点击确定提交教职工信息";
      }
    },
    /** 查询教职工信息列表 */
    getList() {
      this.loading = true;
      console.log("getlist中的queryParams: ", this.queryParams);
      listTeacher(this.queryParams).then((response) => {
        this.teacherList = response.rows;
        console.log("teacherList: ", this.teacherList);
        this.total = response.total;
        this.loading = false;
      });
    },

    //详情
    async showInfo(row) {
      // 首先获取教师的详细信息
      const teacherId = row.teacherId || this.ids;
      await getTeacher(teacherId).then((response) => {
        this.row = response.data;
        console.log("教职工详细信息： ", this.row);
      });
      this.showDialogVisible = true;
      // this.row = row;
      // console.log("预览功能");
    },
    // 性别码字典翻译
    sexFormat(row, column) {
      // 这里不能显示未知，字典中未知的值为-1，但是后端传回的数据中未知不是-1
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 取消按钮
    cancel() {
      this.addActive = 0;
      this.addText = "以下为教职工所在组织的基本信息:";
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        collegeId: null,
        teacherId: null,
        userId: null,
        teacherNo: null,
        teacherName: null,
        idCard: null,
        birthday: null,
        sex: null,
        departmentCenterNo: null,
        departmentNumber: null,
        namePinyin: null,
        nameUsedBefore: null,
        nation: null,
        health: null,
        bloodType: null,
        faiths: null,
        hkNo: null,
        maritalStatus: "0",
        nativePlace: null,
        birthplace: null,
        familyOrigin: null,
        brcf: null,
        homeAddress: null,
        currentAddress: null,
        registeredResidence: null,
        hkxz: null,
        culturalLevel: null,
        workDate: null,
        schoolYear: null,
        teachingYear: null,
        bzlbm: null,
        facultyCategory: null,
        zyh: null,
        teachingStatus: "0",
        fileNumber: null,
        fileText: null,
        photo: null,
        mailingAddress: null,
        tel: null,
        postalCode: null,
        email: null,
        homepage: null,
        specialty: null,
      };
      this.resetForm("form");
      this.closedDialog();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.teacherId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教职工信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const teacherId = row.teacherId || this.ids;
      getTeacher(teacherId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教职工信息";
        // this.termNameOptions[0].id = response.data.termId;
        // this.termNameOptions[0].termName = response.data.termName;
        // this.collegeOptions[0].id = response.data.collegeId;
        // this.collegeOptions[0].name = response.data.collegeName;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.teacherId != null) {
            updateTeacher(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeacher(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const teacherIds = row.teacherId || this.ids;
      this.$confirm(
        '是否确认删除教职工信息编号为"' + teacherIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delTeacher(teacherIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有教职工信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportTeacher(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
